package com.example.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.pojo.Book;
import com.example.service.BookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 定时任务类，定时删除数量小于或等于0的图书
 */
@Component
@Slf4j
public class BookTask {

    @Autowired
    private BookService bookService;

    /**
     * 定时处理超时数量为0的图书
     * 每分钟检查一次图书数量，若存在数量小于或等于0的图书则将其删除
     */
    @Scheduled(cron = "1 * * * * ? ")
    public void processZeroBook() {
        log.info("定时处理数量小于或等于0的图书", LocalDateTime.now());

        //构建删除条件
        LambdaQueryWrapper<Book> wrapper = new LambdaQueryWrapper<Book>()
                .le(Book::getSum, 0);

        //删除图书
        bookService.remove(wrapper);
    }
}
